import { Component, Input, OnInit } from '@angular/core';
import { SFSchema } from '@delon/form';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { _HttpClient } from '@delon/theme';
import { fchmod } from 'fs';

@Component({
  selector: 'app-menu-edit',
  templateUrl: './menu-edit.component.html',
  styles: []
})
export class MenuEditComponent implements OnInit {
  @Input()type: number; // 0 新增 1 修改
  @Input()param: any; // 参数
  showContent =  false;
  schema: SFSchema = {
    properties: {
      menuName: {
        type: 'string',
        title: '菜单名称',

      },
      menuUrl: {
        type: 'string',
        title: '路径'
      },
      menuPermit: {
        type: 'string',
        title: '授权码'
      },
      menuType: {
        type: 'string',
        title: '类型',
        enum: [
          {label: '目录', value: '1'},
          {label: '菜单', value: '2'},
          {label: '按钮', value: '3'}
        ],
        ui: {
          widget: 'select'
        }
      },
      menuParentId: {
        type: 'string',
        title: '上级菜单',
        enum: [],
        ui: {
          widget: 'select',
          allowClear: 'true'
        }
      },
      /*menuIcon: {
        type: 'string',
        title: '图标',
        enum: [
          {label: 'setting', value: 'setting'},
          {label: 'appstore', value: 'appstore'},
        ],
        ui: {
          widget: 'icon-select',
        }
      },*/
      menuIcon: {
        type: 'string',
        title: '图标'
      },
      status: {
        type: 'string',
        title: '状态',
        enum: [
          {label: '启用', value: true},
          {label: '停用', value: false},
        ],
        ui: {
          widget: 'select',

        }
      },
      menuSortno: {
        type: 'number',
        title: '排序'
      },
      menuRemark: {
        type: 'string',
        title: '备注'
      }

    },
    required: ['menuName', 'menuUrl', 'menuPermit', 'menuType', 'status']
  };
  menuList: Array<{label: string, value: string}>
  getMenuList(): void {
    this.http.post(this.api.menu.list.url, {}).subscribe(
      res => {
        console.log(res);
        this.showContent = true;
        if (res.status === '200') {
          let list=res.data.filter(item =>{
            return item.menuType != "3";
          })
          this.menuList = list.map(item => {
            return {value: item.id, label: item.menuName}
          })
          this.schema.properties.menuParentId.enum = this.menuList;
        }
      }
    )
  }
  submit(data: object): void {
    const url = this.type === 0 ? this.api.menu.add.url : this.api.menu.update.url;
    const method = this.type === 0 ? this.api.menu.add.method : this.api.menu.update.method
    this.http.request<any>(method, url, {body:data}).subscribe(res => {
      if (res.status === '201' || res.status === '200') {
        this.msg.success('操作成功');
        this.modalRef.triggerOk();
      } else {
        this.msg.error(res.message)
      }
    })

  }
  constructor(
    private msg: NzMessageService,
    private modalRef: NzModalRef,
    private http: _HttpClient,
    private api: ApiService
  ) { }

  ngOnInit() {
    this.getMenuList();
  }

}
